package com.yx.yinshanjinniu.activitys;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.HotArticleAdapter;
import com.yx.yinshanjinniu.adapters.HotTagAdapter;
import com.yx.yinshanjinniu.adapters.OtherArticleAdapter;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.bean.HomeHotBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.RouteUtil;

import java.util.ArrayList;
import java.util.List;

public class HotTopicActivity extends BaseActivity {

    private HotTagAdapter hotTagAdapter;
    private ArrayList<String> hotTags = new ArrayList<>();
    private ArrayList<HomeHotBean> homeHotBeans = new ArrayList<>();
    private RecyclerView rv_hot_topic, rv_list;
    private HotArticleAdapter hotArticleAdapter;
    private OtherArticleAdapter otherArticleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_hot_topic;
    }

    @Override
    protected void findViews() {
        rv_hot_topic = $(R.id.rv_hot_topic);
        rv_list = $(R.id.rv_list);
        hotTagAdapter = new HotTagAdapter(mContext, null, new HotTagAdapter.OnTagClickListener() {
            @Override
            public void onTagClick(String tag) {
                if (tag.equals("实时咨询")){
                    getAllNews();
                }else {
                    getTagNews(tag);
                }
            }
        });
        rv_hot_topic.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        rv_hot_topic.setAdapter(hotTagAdapter);
        hotArticleAdapter = new HotArticleAdapter(mContext, null);
        hotArticleAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                HomeHotBean hotData = (HomeHotBean) data.get(position);
                RouteUtil.toHotNewsDetailActivity((BaseActivity) mContext, hotData.data.title, hotData.data.content);
            }
        });
        otherArticleAdapter = new OtherArticleAdapter(mContext, null);
        otherArticleAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                HomeHotBean hotData = (HomeHotBean) data.get(position);
                RouteUtil.toHotNewsDetailActivity((BaseActivity) mContext, hotData.title, hotData.content);
            }
        });
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(hotArticleAdapter);

    }

    @Override
    protected void setListeners() {

    }

    private void initData() {
        hotTags.add("实时咨询");
        mAppAction.getHotTagList(new ActionCallbackListener<List<String>>() {
            @Override
            public void onSuccess(List<String> data) {
                hotTags.addAll(data);
                hotTagAdapter.setData(hotTags);
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
        getAllNews();
    }

    private void getAllNews() {
        mAppAction.getLatestNews(new ActionCallbackListener<List<HomeHotBean>>() {
            @Override
            public void onSuccess(List<HomeHotBean> data) {
                homeHotBeans.clear();
                homeHotBeans.addAll(data);
                hotArticleAdapter.setData(homeHotBeans);
                rv_list.setAdapter(hotArticleAdapter);
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    private void getTagNews(String tag) {
        mAppAction.getTagNews(tag, new ActionCallbackListener<List<HomeHotBean>>() {
            @Override
            public void onSuccess(List<HomeHotBean> data) {
                homeHotBeans.clear();
                homeHotBeans.addAll(data);
                otherArticleAdapter.setData(homeHotBeans);
                rv_list.setAdapter(otherArticleAdapter);
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });

    }
}